﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using Entity;
using Command;

namespace DAL
{
    public class X_Shopcart
    {
        //根据id查询图书信息
        public static List<BookInformatica> Getbookinfo(int id)
        {

            string sql = string.Format("select * from BookInformatica where BookId='{0}'",id);
            List<BookInformatica> list = new List<BookInformatica>();
            SqlDataReader dr = DBHelper.Getdata(sql);
            while (dr.Read())
            {
                BookInformatica b = new BookInformatica();
                b.BookId = (int)dr["BookId"];
                b.BookName = (string)dr["BookName"];
                b.BookPrice = (decimal)dr["BookPrice"];
                b.BookPicture = (string)dr["BookPicture"];
                list.Add(b);
            }
            dr.Close();
            return list;
        }
        //查询购物车是否存在已有商品
        public static List<CartItem>  Getcaritem(int bookid, int cid)
        {
            string sql = string.Format("select * from cartitem where BookId={0} and CartId={1}",bookid,cid);
            List<CartItem> list = new List<CartItem>();
            SqlDataReader dr = DBHelper.Getdata(sql);
            while (dr.Read())
            {
                CartItem c = new CartItem();
                c.CartId = (int)dr["CartId"];
                c.Num = (int)dr["Num"];
                c.BookId = (int)dr["BookId"];
                list.Add(c);
            }
            dr.Close();
            return list;
        }
        //根据用户id查询是否有购物车
        public static int Carnum(int id)
        {
            string sql = string.Format("select * from shoppingcart where UserID={0}",id);
            List<ShoppingCart> list = new List<ShoppingCart>();
            int a = 0;
            SqlDataReader dr = DBHelper.Getdata(sql);
            if (dr.Read())
            {
                a = (int)dr["CartId"];
            }
            else
            {
                a = 0;
            }
            dr.Close();
            return a;
        }
        //添加购物车
        public static int Insertcart(int id)
        {
            string sql = string.Format("insert into ShoppingCart ([UserID]) values ('{0}')",id);
            return DBHelper.Adddata(sql);
        }
        //添加购物车详情
        public static int Insertcaritem(CartItem item,int cid)
        { 
        string sql=string.Format("insert into CartItem   ([CartId],[Num],[Price],[Img],[BookName],[BookId]) values ({0},{1},'{2}','{3}','{4}','{5}')",
            cid,item.Num,item.Price,item.Img,item.BookName,item.BookId);
        return DBHelper.Adddata(sql);
            
        }

        //修改购物车
        public static int Updatashop(int num,int bookid,int cartid)
        {
            string sql = string.Format("update [CartItem] set Num='{0}' where BookId='{1}' and CartId='{2}'",num,bookid,cartid);
            return DBHelper.Adddata(sql);
        }
       
        //获取购物车的所有数据
        public static List<CartItem> Get( int uid)

        {

            string sql = string.Format("select * from cartitem where CartId=(select CartId from ShoppingCart where UserID={0})",uid);
            List<CartItem> list = new List<CartItem >();
            SqlDataReader dr = DBHelper.Getdata(sql);
            while (dr.Read())
            {
                CartItem s = new CartItem();
                s.Id = (int)dr["Id"];
                s.CartId = (int)dr["CartId"];
                s.Num = (int)dr["Num"];
                s.BookId = (int)dr["BookId"];
                s.Price = (decimal)dr["Price"];
                s.Img = (string)dr["Img"];
                s.BookName = (string)dr["BookName"];
                list.Add(s);
            }
            dr.Close();
            return list;
        }
      
        //删除
        public static int Delshop(int bookid,int carid)
        {
            string sql = string.Format("delete from CartItem where CartId='{0}' and BookId='{1}'",carid,bookid);
            return DBHelper.Adddata(sql);
        }
    }
}
